﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ufida.u9.ait.openapi.utils;
using UFIDA.U9.Base;
using UFIDA.U9.Base.Organization;
using UFIDA.U9.Base.UOM;
using UFIDA.U9.CBO.MFG.WorkCenter;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.Complete.CompleteRpt;
using UFIDA.U9.MO.Complete;
using UFIDA.U9.MO.Enums;
using UFIDA.U9.MO.MO;
using UFIDA.U9.MO.MOWorkHourBE;
using UFIDA.U9.MO.SFC.DispatchCompleteOrder;
using UFIDA.U9.SM.DealerSO;
using UFSoft.UBF.Business;

namespace ufida.u9.ait.openapi
{
    internal class MOWorkHourBase
    {
        public static MOWorkHourBE CreateMOWorkHour(MOWorkHourAITDTOData dto)
        {
            MOWorkHourBE item;
            using (ISession se = Session.Open())
            {
                item = MOWorkHourBE.Create();
                SetValues(item, dto);
                se.Commit();
            }
            return item;
        }
        public static MOWorkHourBE UpdateMOWorkHour(MOWorkHourBE item, MOWorkHourAITDTOData dto)
        {
            using (ISession se = Session.Open())
            {
                SetValues(item, dto);
                se.Commit();
            }
            return item;
        }

        private static void SetValues(MOWorkHourBE item, MOWorkHourAITDTOData dto)
        {
            //校验
            if (dto.ItemMasterNum == null)
                throw new Exception("ItemMasterNum为空!");
            ItemMaster itemMaster = EntityUtils.GetEntityByCode<ItemMaster>(dto.ItemMasterNum.Code);

            //来源单据处理
            MO mo = null;
            if (dto.SrcDoc != null && dto.SrcDoc.MOSrcDocType == 6)
            {
                //完工报告
                CompleteRpt lydoc = EntityUtils.GetDocEntity<CompleteRpt>(dto.SrcDoc.SrcDocNo);
                item.SrcDoc = new UFIDA.U9.MO.PropertyTypes.MOSourceDoc()
                {
                    SrcDoc = lydoc,
                    SrcDocNo = dto.SrcDoc.SrcDocNo,
                    MOSrcDocType = MOSrcDocTypeEnum.CompleteRpt
                };
                mo = lydoc.MO;
            }
            else if (dto.SrcDoc != null && dto.SrcDoc.MOSrcDocType == 12)
            {
                //派工申报单
                DispatchCompleteOrderDoc lydoc;
                if (dto.SrcDoc.SrcDoc != null)
                {
                    lydoc = DispatchCompleteOrderDoc.Finder.FindByID(dto.SrcDoc.SrcDoc.EntityID);
                    if (lydoc == null) throw new Exception("查找派工申报单失败!");
                }
                else
                {
                    lydoc=EntityUtils.GetDocEntity<DispatchCompleteOrderDoc>(dto.SrcDoc.SrcDocNo);
                }
                item.SrcDoc = new UFIDA.U9.MO.PropertyTypes.MOSourceDoc()
                {
                    SrcDoc = lydoc,
                    SrcDocNo = dto.SrcDoc.SrcDocNo,
                    MOSrcDocType = MOSrcDocTypeEnum.DPCO,
                };
                if (dto.SrcDoc.SrcDocLine != null)
                {
                    DispatchCompleteOrderDocLine line = DispatchCompleteOrderDocLine.Finder.FindByID(dto.SrcDoc.SrcDocLine.EntityID);
                    if (line == null) throw new Exception("查找派工单行失败");
                    item.SrcDoc.SrcDocLine = line;
                    item.SrcDoc.SrcDocLineNo = line.LineNum.ToString();
                    item.MOOperation = line.MOOperation;
                    item.OperationDoc = line.MOOperation.OpDescription;
                    item.OpNum = line.MOOperation.OperationNum;


                }
                
                mo=lydoc.MO;
                

            }
            else if (dto.SrcDoc != null && dto.SrcDoc.MOSrcDocType == 16)
            {
                //完工申报
                CompleteApplyDoc lydoc = EntityUtils.GetDocEntity<CompleteApplyDoc>(dto.SrcDoc.SrcDocNo);
                item.SrcDoc = new UFIDA.U9.MO.PropertyTypes.MOSourceDoc()
                {
                    SrcDoc = lydoc,
                    SrcDocNo = dto.SrcDoc.SrcDocNo,
                    MOSrcDocType = MOSrcDocTypeEnum.CompleteApplyDoc
                };
                if (lydoc.SourceDoc != null && lydoc.SourceDoc.MOSrcDocType == MOSrcDocTypeEnum.MO)
                {
                    mo = MO.Finder.FindByID(lydoc.SourceDoc.SrcDocID);
                }
            }
            else if (dto.MO != null)
            {
                //来源=生产订单
                mo = MO.Finder.Find($"DocNo='{dto.MO.Code}'");
                if (mo == null)
                    throw new Exception($"查找生产订单失败!单号:[{dto.MO.Code}]");
            }

            //物料校验
            if (mo != null)
            {
                if (mo.ItemMaster.ID != itemMaster.ID)
                    throw new Exception($"生产订单的料品[{mo.ItemMaster.Code}]与传入料品[{itemMaster.Code}]不同!");
            }

            //通用赋值
            if (dto.Org != null)
            {
                item.Org = EntityUtils.GetEntityByCode<Organization>(dto.Org.Code);
            }
            else
            {
                item.Org = Context.LoginOrg;
            }
            
            item.MO = mo;
            item.ItemMasterNum = EntityUtils.GetEntityByCode<ItemMaster>(dto.ItemMasterNum.Code);
            item.WorkCenter = EntityUtils.GetEntityByCode<WorkCenter>(dto.WorkCenter);
            if (dto.WorkHourUOM != null)
            {
                item.WorkHourUOM = EntityUtils.GetEntityByCode<UOM>(dto.WorkHourUOM.Code);
            }
            else if (item.ItemMasterNum.MfgInfo != null && item.ItemMasterNum.MfgInfo.TimeUom != null)
            {
                item.WorkHourUOM = item.ItemMasterNum.MfgInfo.TimeUom;
            }
            else
            {
                throw new Exception("工时单位 不能为空");
            }
            
            item.SetupMachineHours = dto.SetupMachineHours;
            item.SetupLaborHours = dto.SetupLaborHours;
            item.RunMachineHours = dto.RunMachineHours;
            item.RunLaborHours = dto.RunLaborHours;
            item.IdleHours = dto.IdleHours;
            item.BusinessDate = DateTime.Parse(dto.BusinessDate);
            
        }
    }
}
